using System.Collections.Generic;

namespace LockStepFrame
{
    public abstract partial class Server : INetModel
    {
        public abstract void RegisterNetEvents();
        public abstract void Send(ICommand command);
    }
    /// <summary>
    /// Net Event Register
    /// </summary>
    public partial class Server
    {
        Dictionary<int, System.Action<ICommand>> eventHandler = new Dictionary<int, System.Action<ICommand>>(512);
        void RegEventHandler(int msgId,System.Action<ICommand> handler)
        {
            eventHandler[msgId] = handler;
        }
    }

    //Net Command Queue
    public partial class Server
    {
        Queue<ICommand> commands = new Queue<ICommand>();
        public void PushCommand(ICommand command)
        {
            commands.Enqueue(command);
        }

    }

    public partial class Server
    {
        StepTimer stepTimer;
        public Server() 
        {
            commands.Clear();
            stepTimer = new StepTimer(TimeOutFrameSync);
            RegisterNetEvents();
        }

        public void Update()
        {
            stepTimer.Update();
        }

        void TimeOutFrameSync()
        {
            while(commands.Count > 0)
            {
                var cmd = commands.Dequeue();
                Send(cmd);
            }
        }
    }
}
